package com.app.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.app.pojo.Company;
import com.app.mapper.CompanyMapper;
import com.app.service.ICompanyService;
import com.app.util.Result;
import com.app.vo.CompanyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 单位信息 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {


    private final CompanyMapper companyMapper;

    public CompanyServiceImpl(CompanyMapper companyMapper) {
        this.companyMapper = companyMapper;
    }

    @Override
    public Company getCompanyByCompanyName(String companyName) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("company_name", companyName);
        return this.getOne(queryWrapper);
    }

    /**
     * 咨询电话/单位新增修改接口
     *
     * @param company
     * @return
     */
    @Override
    public Result companyAddAndUpdate(Company company) {
        if (company.getId() == null) {//新增
            company.setCreateTime(LocalDateTime.now());
            company.setStatus(1);
            this.save(company);
        } else {
            company.setUpdateTime(LocalDateTime.now());
            this.updateById(company);
        }
        return Result.success("操作成功");
    }

    /**
     * Admin单位公司列表查询
     *
     * @return
     */
    @Override
    public Result<CompanyVo> companyList(Integer page, Integer limit, String keyword) {
        Page<Company> companyPage = new Page<>(page, limit);
        Page<Company> companyList = companyMapper.selectPage(companyPage, new QueryWrapper<Company>()
                .like(StrUtil.isNotBlank(keyword), "company_name", keyword)
                .eq("status", 1));
        CompanyVo companyVo = new CompanyVo();
        companyVo.setList(companyList.getRecords());
        companyVo.setCount((int) companyList.getTotal());
        return Result.success(companyVo);
    }

    /**
     * mini查询单位列表
     * @return
     */
    @Override
    public Result<List<Company>> getList() {
        List<Company> companyList = companyMapper.selectList(new QueryWrapper<Company>()
                .eq("status", 1));
        return Result.success(companyList);
    }

}
